package com.jiayang.hhotel.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.SpecialRoomStateHistoryListAdapter;
import com.jiayang.hhotel.adapter.SpecialRoomStateListAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.SpecialRoomStateModel;
import com.jiayang.hhotel.presenter.SpecialRoomStateHisPresenter;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.base.ActivityCollector;
import cn.droidlover.xdroidmvp.custom.SeatImgListener;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/6/6 0006.
 * 特殊房态历史页面
 */

public class SpecialRoomStateHisActivity extends XActivity<SpecialRoomStateHisPresenter> implements View.OnClickListener, DateUtil.PickerDeterListener {


    private View layout;
    private ImageView icon;
    private TextView tv;

    private ImageView back;

    private EditText input;
    private Button search;
    private ImageView calendar;

    private SwipeMenuRecyclerView recyclerView;
    private SwipeRefreshLayout fresh;

    private SpecialRoomStateHistoryListAdapter adapter;

    private int page;

    @Override
    public void initData(Bundle savedInstanceState) {
        fresh = (SwipeRefreshLayout) findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);
        back = (ImageView) findViewById(R.id.tv_back);
        input = (EditText) findViewById(R.id.et_search);
        search = (Button) findViewById(R.id.bt_search);
        calendar = (ImageView) findViewById(R.id.iv_date);

        isFresh(true);
        isLoadMore = true;
        getP().getList(Constant.LAUGUAGE_FLAG_CHINESE,1,1,page);
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isLoadMore){
                    page=1;
                    getP().getList(Constant.LAUGUAGE_FLAG_CHINESE,1,1,page);
                }else{
                    isFresh(false);
                }
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    // TODO 这里有个注意的地方，如果你刚进来时没有数据，但是设置了适配器，这个时候就会触发加载更多，需要开发者判断下是否有数据，如果有数据才去加载更多。
                    if(!isLoadMore){
                        isLoadMore = true;
                        page++;
                        getP().getList(Constant.LAUGUAGE_FLAG_CHINESE,1,1,page);
                    }
                }
            }
        });

        back.setOnClickListener(this);
        calendar.setOnClickListener(this);
    }

    public void setAdapter(List<SpecialRoomStateModel.PAssignRoomListDtosBean> list){
        isFresh(false);
        if(adapter==null){
            adapter = new SpecialRoomStateHistoryListAdapter(this,list);
            recyclerView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
    }

    private boolean isLoadMore;//判断是否正在加载更多。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        isFresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            fresh.setVisibility(View.GONE);
            initSeatPic(SpecialRoomStateHisActivity.this, true, StringUtil.errMsg(state), Constant.SEAL_NET_ERROR_IMG, new SeatImgListener() {
                @Override
                public void OnSeatClick() {
                    switch (state){
                        case 1002://没有数据
                            icon.setImageResource(Constant.NO_DATA_PICTURE);
                            tv.setText(getString(R.string.no_data));
                            icon.setOnClickListener(null);
                            break;
                        case -1://服务器错误
                            icon.setImageResource(Constant.NET_ERROR_PICTURE);
                            tv.setText(getString(R.string.service_error_contract_manager));
                            icon.setOnClickListener(null);
                            break;
                        default://其他错误
                            icon.setImageResource(Constant.TIME_OUT_PICTURE);
                            tv.setText(getString(R.string.service_error));
                            icon.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    fresh.setVisibility(View.VISIBLE);
                                    layout.setVisibility(View.GONE);
                                    isFresh(true);
                                    getP().getList(Constant.LAUGUAGE_FLAG_CHINESE,1,1,page);
                                }
                            });
                            break;
                    }
                }
            });
        }else{//之前请求到有数据
            ToastUtil.Toast(StringUtil.errMsg(state),this);
        }
    }

    private void isFresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_special_room_state_history;
    }

    @Override
    public SpecialRoomStateHisPresenter newP() {
        return new SpecialRoomStateHisPresenter();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_back://回退
                ActivityCollector.removeActivity(this);
                finish();
                break;
            case R.id.iv_date://日历
                DateUtil.initDatePicker(this,this);
                break;
        }
    }

    @Override
    public void onPicker(String date) {

    }
}
